<template>
  <div class="system-user-container layout-padding">
    <el-card class="layout-padding-auto" shadow="hover">
      <div class="system-user-search mb15">
        <el-input placeholder="请输入用户名称" size="default" style="max-width: 180px"></el-input>
        <el-button v-loading="tableData.isLoading" class="ml10" size="default" type="primary" @click="tableData.fetchData()">
          <el-icon>
            <ele-Search/>
          </el-icon>
          查询
        </el-button>
        <el-button class="ml10" size="default" type="success" @click="onOpenAddUser('add')">
          <el-icon>
            <ele-FolderAdd/>
          </el-icon>
          新增用户
        </el-button>
      </div>
      <el-table v-loading="tableData.isLoading" :data="tableData.data" style="width: 100%">
        <el-table-column label="序号" type="index" width="60"/>
        <el-table-column label="账户名称" prop="userName" show-overflow-tooltip></el-table-column>
        <el-table-column label="头像" prop="avatar_url" show-overflow-tooltip>
          <template #default="scope">
            <el-image :src="scope.row.avatar_url" style="width: 36px;height: 36px"/>
          </template>
        </el-table-column>
        <el-table-column label="登录ip" prop="last_login_ip" show-overflow-tooltip></el-table-column>
        <el-table-column :formatter="formatTime" label="登录时间" prop="last_login_time" show-overflow-tooltip></el-table-column>
        <el-table-column label="手机号" prop="phone" show-overflow-tooltip></el-table-column>
        <el-table-column label="邮箱" prop="email" show-overflow-tooltip></el-table-column>
        <el-table-column label="错误" prop="error_num" show-overflow-tooltip></el-table-column>
        <el-table-column label="用户状态" prop="status" show-overflow-tooltip>
          <template #default="scope">
            <el-tag v-if="scope.row.status" type="success">启用</el-tag>
            <el-tag v-else type="info">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="用户描述" prop="description" show-overflow-tooltip></el-table-column>
        <el-table-column :formatter="formatTime" label="创建时间" prop="create_time" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <!--            <el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary" @click="onOpenEditUser('editPwd', scope.row)">修改密码</el-button>-->
            <el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary" @click="onOpenEditUser('edit', scope.row)">修改</el-button>
            <el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="tableData.pagination.current"
          v-model:page-size="tableData.pagination.pageSize"
          :page-sizes="tableData.pagination.pageList"
          :pager-count="5"
          :total="tableData.pagination.total"
          background
          class="mt15"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="tableData.handleSizeChange"
          @current-change="tableData.handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <UserDialog ref="userDialogRef" @refresh="tableData.fetchData()"/>
  </div>
</template>

<script lang="ts" name="systemUser" setup>
import {defineAsyncComponent, onMounted, reactive, ref} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus';
import usePagination from "/@/utils/usePagination";
import {useBaseApi} from "/@/api/base";
import {formatTime} from "/@/utils/formatTime";

// 引入组件
const UserDialog = defineAsyncComponent(() => import('/@/views/system/user/dialog.vue'));

// 定义变量内容
const userDialogRef = ref();
const path = ref('user');
const whereData = reactive({})
const tableData = usePagination({
  path: path.value,
  page: undefined,
  size: undefined
}, whereData)

// 打开新增用户弹窗
const onOpenAddUser = (type: string) => {
  userDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditUser = (type: string, row: RowUserType) => {
  userDialogRef.value.openDialog(type, row);
};
// 删除用户
const onRowDel = (row) => {
  ElMessageBox.confirm(`此操作将商品：“${row.title}放入回收站”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    useBaseApi().del(path.value, row.id).then(res => {
      tableData.fetchData()
      ElMessage.success('删除成功')

    })
  }).catch(() => {
  })
};
// 分页改变

// 分页改变

// 页面加载时
onMounted(() => {
  tableData.fetchData()
});
</script>

<style lang="scss" scoped>
.system-user-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}
</style>
